/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.common  
 */
package com.apache.passport.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.passport.entity.LoginInfo;

/**
 * description:  文件数据操作类
 * @author Hou Dayu 创建时间：2015-1-16  
 * @Copyright(c)2014:
 */
public class FileUtils {
	private Logger log = Logger.getLogger(FileUtils.class);
	private static FileUtils instance;
	private String CRLGINFILEPATH = "";

	private FileUtils() {
		/**
		 * 文件路径,文件名称为:passport_YYYYMMDD.papt
		 * 用户名	userEname
		 * 	最后登录介质	lastLgnDevice
		 * 	最后登录时间	lastLgnTime
		 * 	当前登录时间	currLgnTime
		 * 	登录系统名称	SysName
		 * 	最后登录IP地址	lastLgnIpAddr
		 */
		CRLGINFILEPATH = SystemTools.getInstance().getValue("crLginFilePath");
	}

	public static FileUtils getInstance() {
		if (null == instance) {
			instance = new FileUtils();
		}
		return instance;
	}

	/**
	 * description:  创建用户登录日志文件
	 * @param param
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-19
	 */
	public boolean createLoginFile(ParamsVo<LoginInfo> param) {
		File file = new File(CRLGINFILEPATH);
		boolean flag = false;
		try {
			if (file.isFile()) {
				log.info("warning: [crLginFilePath非路径],请检查unity_config.properties配置");
			} else {
				if (file.exists()) {
					flag = file.mkdirs();
				}
				String nowdate = new SimpleDateFormat("yyyyMMdd").format(new Date());
				String fileName = CRLGINFILEPATH + File.separator + "passport_" + nowdate + ".papt";
				file = new File(fileName);
				if (!file.exists()) {
					flag = file.createNewFile();
				}
				//5M以上,重新生成新文件
				long fileSize = file.length();
				String size = formetFileSize(fileSize);
				double dobSize = Double.parseDouble(size);
				//				if (dobSize > 5.0) {
				//					fileName = CRLGINFILEPATH + File.separator + "passport_" + nowdate + ".papt";
				//				}
				LoginInfo loginInfo = (LoginInfo) param.getObj();
				StringBuffer sb = new StringBuffer();
				sb.append(loginInfo.getSessionId()).append("|");
				sb.append(loginInfo.getTokenId()).append("|");
				sb.append(loginInfo.getUserEname()).append("|");
				sb.append(loginInfo.getLgnLastdevice()).append("|");
				sb.append(loginInfo.getLgnLasttime()).append("|");
				sb.append(loginInfo.getLgnLasttime()).append("|");//注意,当前登录时间暂时为最后登录时间
				sb.append(loginInfo.getSysEname()).append("|");
				sb.append(loginInfo.getLgnLastipaddress()).append("|");
				flag = write(file, sb.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * description:  写本地文件
	 * @param fileName
	 * @param Content
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-19
	 */
	private boolean write(File fileName, String Content) {
		boolean flag = false;
		FileOutputStream path = null;
		PrintStream p = null;
		try {
			path = new FileOutputStream(fileName, true);
			p = new PrintStream(path);
			p.println(Content);
			flag = true;
		} catch (Exception e) {
			flag = false;
		} finally {
			try {
				if (!Validator.isEmpty(p)) {
					p.close();
				}
				if (!Validator.isEmpty(path)) {
					path.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	/**
	 * description:  获取文件大小
	 * @param fileS
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-19
	 */
	private String formetFileSize(long fileSize) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileSize < 1024) {
			fileSizeString = df.format((double) fileSize);
		} else if (fileSize < 1048576) {
			fileSizeString = df.format((double) fileSize / 1024);
		} else if (fileSize < 1073741824) {
			fileSizeString = df.format((double) fileSize / 1048576);
		} else {
			fileSizeString = df.format((double) fileSize / 1073741824);
		}
		return fileSizeString;
	}
}
